/*
 * @Author: wang cong wangc9832@163.com
 * @Date: 2023-11-27 15:18:19
 * @LastEditors: wang cong wangc9832@163.com
 * @LastEditTime: 2023-11-27 16:45:56
 * @FilePath: /ams-front/src/hooks/web/useReloadTableHook.ts
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import { ref, onActivated, Ref } from 'vue';
/*被激活过的页面重新加载 
// 使用自定义 Hook，传入一个functon
const { isActivated } = useReloadTableHook(reload);
*/
interface UseReloadTableHook {
  isActivated: Ref<boolean>;
}

type ReloadTableFunction = (page?: any) => void;
type GetPaginationRef = () => any | undefined;

export function useReloadTableHook(
  reloadTableFunction: ReloadTableFunction,
  getPaginationRef?: GetPaginationRef
): UseReloadTableHook {
  const isActivated = ref(false);

  onActivated(() => {
    if (isActivated.value && typeof reloadTableFunction === 'function') {
      const page = getPaginationRef?.();
      page ? reloadTableFunction(page) : reloadTableFunction();
    }
    isActivated.value = true;
  });

  return {
    isActivated,
  };
}
